import QtQuick
import QtQuick.Controls
import QtQuick.Effects
import QtQuick.Layouts
import "../component" as C
import "../component/ZGlobal.js" as G

Item {
    id: rootStirPage
    anchors.fill: parent
    // Image {
    //     id: backgroundImage
    //     anchors.fill: parent
    //     source: "qrc:/assets/page_bg.png"  // 图片路径
    //     fillMode: Image.PreserveAspectCrop  // 保持宽高比并填充
    //     // opacity: 0.8  // 可选：设置透明度
    // }
    FontLoader {
        id: iconFont
        source: "qrc:/assets/fontawesome-free-6.7.2-desktop/otfs/Font Awesome 6 Free-Solid-900.otf"
    }
    property int    speed: 0
    property int    second: 0
    property string status: collectionInfo.speed>0?"run":"stop"      // run/stop

    Timer {
        id: countTimer
        interval: 1000
        repeat: true
        onTriggered:{
            if (rightPanel.controlMode === "auto"){
                rootStirPage.second > 0 ? rootStirPage.second-- : rootStirPage.stop()
            }
            if (rightPanel.controlMode === "manual"){
                rootStirPage.second++
            }

        }
    }
    function stop(){
        collectionInfo.setSpeed(0);
        countTimer.stop();
        status = "stop"
    }

    // 主题
    Theme { id: theme }
    Image {
        id: knobBackgroundContent
        x: 337
        y: 103
        width:980
        height:832

        source: "qrc:/assets/jbbg.png"
        fillMode: Image.PreserveAspectFit
        smooth: true
    }
    KnobControl {
        id: knob
        anchors.top: parent.top
        anchors.topMargin: 75
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.horizontalCenterOffset: -56
        width: 800
        height: 1000
        value: collectionInfo.speed
        minValue: 0
        stepSize: 1
        maxValue: 600
        icon: "SV"
        unit: "rpm"
        title: "rpm"
        setTitle: "设定转速:"
        incDecEnabled: false

        onValueModified: function(newValue) {
            console.log("新转速值:", newValue)
            speed = newValue
        }
    }
    Row {
        id: controlRow
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 80
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.horizontalCenterOffset: -56
        spacing: 20
        Item {
            width: 70
            height: 70
            Image { anchors.fill: parent;
                source: rootStirPage.status === "stop" ? "qrc:/assets/button_a_icon_pause_color.png" : "qrc:/assets/button_a_icon_pause.png";
                fillMode: Image.PreserveAspectFit; smooth: true
            }
            MouseArea{
                anchors.fill: parent
                onClicked:{
                    if (rootStirPage.status === "stop"){
                        if (rootStirPage.speed ===0){
                            // collectionInfo.showInfo("转速为0不能启动！")
                            infoPage.item.msg="转速为0不能启动！"
                            infoPage.item.visible=true;
                            return;
                        }
                        if (rightPanel.controlMode === "auto" && rootStirPage.second ===0){
                            // collectionInfo.showInfo("自动模式下需输入时间！")
                            infoPage.item.msg="自动模式下需输入时间！"
                            infoPage.item.visible=true;
                            return;
                        }


                        rootStirPage.status =  "run";
                        countTimer.start();
                        collectionInfo.setSpeed(speed);
                    }
                }
            }
        }
        Item {
            width: 70
            height: 70
            Image { anchors.fill: parent;
                source: rootStirPage.status === "stop" ? "qrc:/assets/button_a_icon_stop.png":"qrc:/assets/button_a_icon_stop_color.png";
                fillMode: Image.PreserveAspectFit; smooth: true
                MouseArea{
                    anchors.fill: parent
                    onClicked:{
                        if (rootStirPage.status === "run"){
                            rootStirPage.status =  "stop";
                            collectionInfo.setSpeed(0);
                            countTimer.stop();
                        }
                    }
                }
            }
        }
        Item {
            width: 260
            height: 70
            Image { anchors.fill: parent; source: "qrc:/assets/time_display_bg.png"; fillMode: Image.Stretch; smooth: true }
            Text {
                anchors.centerIn: parent;
                text: G.secondsToHMS(rootStirPage.second);
                font.pixelSize: 24; color: theme.focusColor }
        }
    }
    Loader {
        id: motorSettingPage
        source: "qrc:/pages/RangeSettingPage.qml"
        onLoaded: {
            item.title= "电机设置"
            item.type="speed"
        }
    }

    C.ZButtonImage{
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.margins: 20
        btnImagePressed: "qrc:/assets/icon_settings_parameters_pressed.png"
        btnImage:"qrc:/assets/settings_parameters.png"
        onClicked: {
            var speedParam = alarmInfo.getSpeedParam();
            motorSettingPage.item.setValue(speedParam["hh"],
                                           speedParam["h"],
                                           speedParam["l"],
                                           speedParam["ll"],
                                           speedParam["hhStatus"],
                                           speedParam["hStatus"],
                                           speedParam["lStatus"],
                                           speedParam["llStatus"])
            motorSettingPage.item.visible = true;
        }
    }


    // 右侧控制面板
    Rectangle {
        id: rightPanel
        width: 540
        height: 300
        radius: 90
        color: "#FFFFFF"
        anchors.right: parent.right
        anchors.rightMargin: panelCollapsed ? -(width - 60) : -90
        anchors.verticalCenter: parent.verticalCenter

        layer.enabled: true
        layer.effect: MultiEffect {
            shadowEnabled: true
            shadowColor: theme.shadowColor
            shadowBlur: theme.shadowBlur
            shadowHorizontalOffset: theme.shadowXOffset
            shadowVerticalOffset: theme.shadowYOffset
        }

        // 收回
        property bool panelCollapsed: true
        Behavior on anchors.rightMargin { NumberAnimation { duration: 250; easing.type: Easing.OutCubic } }

        property string controlMode: "manual"

        // 双箭头
        Rectangle {
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            anchors.leftMargin: 10
            width: 50
            height: 50
            radius: 12
            color: "transparent"
            Text {
                anchors.centerIn: parent
                text: rightPanel.panelCollapsed ? "\uf100" : "\uf101" // 左/右双箭头
                font.family: iconFont.name
                font.pixelSize: 26
                color: theme.focusColor
            }
            MouseArea {
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onClicked: rightPanel.panelCollapsed = !rightPanel.panelCollapsed
            }
        }

        ColumnLayout {
            anchors.fill: parent
            anchors.margins: 24
            spacing: 18

            // 顶部：自动 / 手动 分段切换
            Item {
                Layout.alignment: Qt.AlignHCenter
                Layout.preferredWidth: 360
                Layout.preferredHeight: 110

                Item {
                    id: segmentBg
                    width:360
                    height: 110
                    anchors.horizontalCenter: parent.horizontalCenter
                    clip: false

                    Image {
                        id: tabImage
                        anchors.fill: parent
                        source: rightPanel.controlMode === "auto" ? "qrc:/assets/slices/tab_auto.png" : "qrc:/assets/slices/tab_manual.png"
                        fillMode: Image.PreserveAspectFit
                        smooth: true
                        opacity: 1.0
                        Behavior on opacity { NumberAnimation { duration: 180; easing.type: Easing.InOutQuad } }
                        MouseArea {
                            anchors.fill: parent
                            cursorShape: Qt.PointingHandCursor
                            onPressed: tabImage.opacity = 0.6
                            onReleased: tabImage.opacity = 1.0
                            onClicked: rightPanel.controlMode = (rightPanel.controlMode === "auto" ? "manual" : "auto")
                        }
                    }

                    Text {
                        text: "自动"
                        font.pixelSize: 20
                        color: rightPanel.controlMode === "auto" ? "white" : "#A7B3C3"
                        anchors.left: parent.left
                        anchors.leftMargin: 90
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.verticalCenterOffset: 12
                        z: 2
                        opacity: tabImage.opacity
                    }
                    Text {
                        text: "手动"
                        font.pixelSize: 20
                        color: rightPanel.controlMode === "manual" ? "white" : "#A7B3C3"
                        anchors.right: parent.right
                        anchors.rightMargin: 75
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.verticalCenterOffset: 12
                        z: 2
                        opacity: tabImage.opacity
                    }
                }
            }

            // 时间设定区域
            Row {
                id: timeRow
                Layout.alignment: Qt.AlignHCenter
                Layout.topMargin: -20
                spacing: 16
                height: 50
                
                // 分钟输入
                Item {
                    visible: rightPanel.controlMode === "auto"
                    width: 120
                    height: 50
                    Image {
                        anchors.fill: parent
                        source: "qrc:/assets/slices/input_box@2x.png"
                        fillMode: Image.Stretch
                        smooth: true
                    }
                    TextField {
                        id: minuteInput
                        anchors.fill: parent
                        anchors.margins: 8
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: "0"
                        font.pixelSize: 22
                        color: "#333333"
                        validator: RegularExpressionValidator {
                            regularExpression: /^([0-5]?[0-9])$/
                        }
                        background: Rectangle { color: "transparent" }
                    }
                }

                Item {
                    width: 24
                    height: 50
                    Text {
                        visible: rightPanel.controlMode === "auto"
                        anchors.centerIn: parent
                        text: ":"
                        font.pixelSize: 28
                        color: "#6B7F99"
                        verticalAlignment: Text.AlignVCenter
                    }
                }

                // 秒输入
                Item {
                    visible: rightPanel.controlMode === "auto"
                    width: 120
                    height: 50
                    Image {
                        anchors.fill: parent
                        source: "qrc:/assets/slices/input_box@2x.png"
                        fillMode: Image.Stretch
                        smooth: true
                    }
                    TextField {
                        id: secondInput
                        anchors.fill: parent
                        anchors.margins: 8
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: "0"
                        font.pixelSize: 22
                        color: "#333333"
                        // validator: IntValidator { bottom: 0; top: 59 }
                        validator: RegularExpressionValidator {
                            regularExpression: /^([0-5]?[0-9])$/
                        }
                        background: Rectangle { color: "transparent" }
                    }
                }
            }

            // 操作按钮
            Row {
                spacing: 16
                Layout.alignment: Qt.AlignHCenter
                Rectangle {
                    width: 120
                    height: 50
                    radius: 12
                    color: "#FFFFFF"
                    border.width: 1
                    border.color: '#ababab'
                    Text {
                        anchors.centerIn: parent
                        text: "取消"
                        font.pixelSize: 20
                        color: '#9a9a9a'
                    }
                    MouseArea {
                        anchors.fill: parent
                        cursorShape: Qt.PointingHandCursor
                        onClicked: {
                            // minuteInput.text = "10"
                            // secondInput.text = "00"
                            rightPanel.panelCollapsed = true
                        }
                    }
                }
                Rectangle {
                    width: 120
                    height: 50
                    radius: 12
                    color: theme.focusColor
                    Text {
                        anchors.centerIn: parent
                        text: "确定"
                        font.pixelSize: 20
                        color: "#FFFFFF"
                    }
                    MouseArea {
                        anchors.fill: parent
                        cursorShape: Qt.PointingHandCursor
                        onClicked: {
                            //collectionInfo.setSpeed(speed);
                            if (rightPanel.controlMode === "auto"){
                                rootStirPage.second = parseInt(minuteInput.text)*3600+parseInt(secondInput.text)*60
                                if (rootStirPage.second ===0){
                                    infoPage.item.msg="自动模式需输入时间,方能确定！"
                                    infoPage.item.visible=true;
                                    return
                                }

                                console.log("second", rootStirPage.second)
                            }else{
                                rootStirPage.second=0
                            }
                            operationInfo.insertOperationLog("设置搅拌模式", "设置为"+rightPanel.controlMode === "auto"?"自动":"手动",rootStirPage.second+"")
                            rightPanel.panelCollapsed = true
                        }
                    }
                }
            }
        }
    }
}
